package com.restaurant.application.consumption;

import com.nebutown.base.ViewSpec;
import com.nebutown.base.data.View;
import com.nebutown.commonunits.dataface.DataModes;
import com.nebutown.commonunits.dataface.EntryTypes;
import com.nebutown.commonunits.dataface.FormShell;
import com.nebutown.commonunits.dataface.definitions.PageForm;
import com.nebutown.commonunits.uidataface.UIDataFace;
import com.nebutown.commonunits.utils.DateTimeUtils;
import com.restaurant.application.accumulate.Accumulate;
import com.restaurant.application.accumulate.support.AccumulateData;
import com.restaurant.application.consumption.support.AbstractConsumption;
import com.restaurant.application.consumption.support.ConsumptionData;
import com.restaurant.application.customer.Customer;
import com.restaurant.application.indexes.consumptionlist.mysql.ConsumptionListBean;
import com.restaurant.application.indexes.consumptionlist.mysql.ConsumptionListImpl;
import com.restaurant.application.scoreset.ScoreSet;

public class ConsumptionImpl extends AbstractConsumption {
    @Override
    public View getFullView() {
        return newCommonTable();
    }

    @Override
    public View getViewLead(ViewSpec viewSpec) {
        return null;
    }

    @Override
    public ConsumptionData getConsumeData() {
        return getData().duplicate();
    }

    @Override
    public void init(ConsumptionData data) {
        data.setShopID("01");
        Double finalPrice = data.getFinalPrice();
        // 计算积分
        int scoreByPaying = getSingleton(ScoreSet.class).findScoreByPaying(data.getShopID(), finalPrice);
        // 创建积分基元
        AccumulateData accumulateData = new AccumulateData().withAccumulate((long) (scoreByPaying)).withAction("消费增加")
                .withType(Accumulate.ACCUMULATE_INCREASE_CONSUMER).withOrderId(data.getOrderId()).withCreateTime(data.getTime())
                .withCustomerUID(data.getCustomer()).withDetail(getId());
        createNode(Accumulate.class).init(accumulateData);
        data.withAccumulatePoints((long) scoreByPaying);
        setData(data);
        getUnit(Customer.class, data.getCustomer()).plusScores(scoreByPaying);
        // 插入大表单
        ConsumptionListBean consumptionListBean = new ConsumptionListBean().withConsumptionUid(getId())
                .withCustomerUid(data.getCustomer()).withConsumptionUid(getId())
                .withWeChatId(data.getWeChatId()).withAccumulatePoints((long) scoreByPaying)
                .withTime(data.getTime()).withFinalPrice(data.getFinalPrice());
        getIndex(ConsumptionListImpl.class).insert(consumptionListBean);
    }


    private View newCommonTable() {
        PageForm pageForm = newFormShell(getData().duplicate()).getForm();
        return getSingleton(UIDataFace.class).newGenericForm(unitPosition(), pageForm);
    }


    private FormShell newFormShell(ConsumptionData data) {

        FormShell formShell = new FormShell("客户消费详情", 3, 2);

        formShell.source(data, DataModes.SHOW);

        formShell.at(0, 0, "客户名称")
                .getEntry(ConsumptionData.FN_SHOPID, data.getShopID())
                .placeHolder("客户名")
                .simpleEntry(EntryTypes.STRING)
                .getContent().setEditable(false);

        formShell.at(0, 1, "消费门店")
                .getEntry(ConsumptionData.FN_SHOPID, data.getShopID())
                .simpleEntry(EntryTypes.STRING)
                .getContent().setEditable(true);

        formShell.at(1, 0, "时间")
                .getEntry(ConsumptionData.FN_TIME, DateTimeUtils.toTime(data.getTime()))
                .simpleEntry(EntryTypes.STRING)
                .getContent().setEditable(false);

        formShell.at(1, 1, "金额")
                .getEntry(ConsumptionData.FN_FINALPRICE, data.getFinalPrice().toString())
                .simpleEntry(EntryTypes.DOUBLE)
                .getContent().setEditable(false);

        formShell.at(2, 0, "微信Id")
                .getEntry(ConsumptionData.FN_WECHATID, data.getWeChatId())
                .simpleEntry(EntryTypes.STRING)
                .getContent().setEditable(false);

        formShell.at(2, 1, "获得积分")
                .getEntry(ConsumptionData.FN_ACCUMULATEPOINTS, data.getAccumulatePoints().toString())
                .simpleEntry(EntryTypes.STRING)
                .getContent().setEditable(false);

        return formShell;
    }
}
